var payModal = $("#pay_modal").modal("收款", function () {
    $("#pay_form").submit();
}, {
    offset: '20px'
});

var editTreatmentModal = $("#add_treatment_modal").modal("疗程编辑", function () {
    $("#treatment_edit_form").submit();
});

var treatmentHandler = {
    showEditModal: function (treatmentId, orderId) {
        editTreatmentModal.show(function () {
            $("#treatment_order_id").val(orderId);
            $("#treatment_id").val(treatmentId);
            $("#treatment_title").val($("#treatment_title_" + treatmentId).val());
            $("#treatment_remark").val($("#treatment_remark_" + treatmentId).text());

            if (treatmentId == '') {
                $(".layui-layer-title").html("添加疗程");
            }
            $("#treatment_edit_form").validateCallback(function () {
                treatmentHandler.saveTreatment();
            }, {
                messages: {
                    treatment_title: "请输入疗程名称"
                }
            })
        });
    },
    saveTreatment: function () {
        var treatmentTitle = $.trim($("#treatment_title").val());
        var remark = $("#treatment_remark").val();

        var requestData = {
            id: $("#treatment_id").val(),
            orderId: $("#treatment_order_id").val(),
            title: treatmentTitle,
            remark: remark
        };

        hot.ajax(treatmentApiUri + "/save", requestData, function (apiResult) {
            if (apiResult.resultCode == 2000) {
                hot.tip.success("保存成功", function () {
                    window.location.reload();
                })
            } else {
                hot.tip.error("保存失败");
            }
        }, function () {
        }, "post", 300);
    },
    payModal: function (treatmentId, amount, payedAmount, notAuditAmount) {
        payModal.show(function () {
            var surplusAmount = amount - payedAmount - notAuditAmount;
            $("#pay_treatmentId").val(treatmentId);
            $("#pay_amount").val(surplusAmount);
            $("#hdSurplusAmount").val(surplusAmount);

            $("#pay_form").validateCallback(function () {
                treatmentHandler.pay();
            });
        });
    },
    pay: function () {
        var treatmentId = $("#pay_treatmentId").val();
        var receiptAccountId = $("#pay_account").val();
        var amount = $("#pay_amount").val();
        var payer = $.trim($("#pay_payer").val());
        var payerMobile = $.trim($("#pay_payerMobile").val());
        var remark = $("#pay_remark").val();

        var requestData = {
            treatmentId: treatmentId,
            receiptAccountId: receiptAccountId,
            amount: amount,
            payer: payer,
            payerMobile: payerMobile,
            remark: remark
        };

        hot.ajax(treatmentApiUri + "/pay", requestData, function (apiResult) {
            if (apiResult.resultCode === 2000) {
                hot.tip.success("操作成功", function () {
                    window.location.reload();
                });
            } else {
                hot.tip.error('操作失败--' + apiResult.resultMsg);
            }
        }, function () {
        }, "post", 300);
    },
    taskList: function (treatmentId) {
        hot.newTab(taskListUri + "?treatmentId=" + treatmentId, treatmentId + "-任务列表");
    }
};